var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: 0,
    orderInfo: {}, // 订单详情
    orderGoods: {}, // 订单商品
    refund: {
      orderSn: '',
      amount: '',
      reason: '',
      remark: ''
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
    this.getOrderDetail();
  },
  getOrderDetail() {
    let that = this;
    util.request('order/detail', {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.code === 0) {
        that.setData({
          orderInfo: res.orderInfo,
          orderGoods: res.orderGoods
        })
      }
    });
  },
  validate(refund) {
    if (!refund.amount) {
      util.showMsg('请填写申请退款金额')
      return false;
    }
    if (!refund.reason) {
      util.showMsg('请填写申请退款原因')
      return false;
    }
    if (!refund.remark) {
      util.showMsg('请输入退款说明')
      return false;
    }
    return true;
  },
  goodsDetail(e) {
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + e.currentTarget.dataset.id,
    })
  },
  changeAmount: function (e) {
    let refund = this.data.refund
    refund.amount = e.detail.value
    this.setData({
      refund: refund
    })
  },
  changeReason:function(e){
    let refund = this.data.refund
    refund.reason = e.detail.value
    this.setData({
      refund: refund
    })
  },
  changeRemark:function(e){
    let refund = this.data.refund
    refund.remark = e.detail.value
    this.setData({
      refund: refund
    })
  },
  onPost() {
    let that = this;
    let refund = that.data.refund
    refund.orderSn = that.data.orderInfo.orderSn
    that.setData({
      refund: refund
    })
    if (!that.validate(that.data.refund)) {
      return;
    }
    util.request('order/applyRefund', that.data.refund, 'POST').then(function (res) {
      if (res.code === 0) {
        util.showMsg('申请成功，等待管理员审核');
        setTimeout(function () {
          wx.navigateBack({
            delta: 2,
            fail: (res) => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          });
        }, 2000);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
